<?php

/* @var $this yii\web\View */

use yii\helpers\Url;
use yii\bootstrap\ActiveForm;

$this->title = '拓盟科技';


$upload = Url::toRoute(['file/upload']);
$submit = Url::toRoute(['file/submit']);
$path = Url::to(['/files/upload']);
$url = Url::home(true).'files/upload';
$this->registerJs("let config = { Upload: '$upload', Path: '$path', Url: '$url', };",\yii\web\View::POS_BEGIN);

$js = <<<JS

let fbx_data = null;
let image_data = null;
function submit_file(){
//alert(fbx_data.url);
  //  window.location.reload();
   $.post({
           url:"$submit", 
           data:{fbx:fbx_data, image:image_data},
           success:function(data){
           window.location.reload();
           /*alert(data);*/},
           fail:function(data){/*alert(data)*/},
       }
   );
}
function print_fbx(msg){

   $("#uploadform-fbx").prev().prev().html(msg);
}
function print_image(msg){

   $("#uploadform-image").prev().prev().html(msg);
}
function fbx_submit(md5, title, type){
    
    print_fbx('处理完成');
    fbx_data = {};
    fbx_data.md5 = md5;
    fbx_data.url = config.Url + "/" + md5;
  //  alert(fbx_data.url);
    fbx_data.title = title;
    fbx_data.type = type;
    if(fbx_data != null && image_data != null){
        $("#submit").prop('disabled', false);
    }
}
function image_submit(md5, title, type){
    
    print_image('处理完成');
    image_data = {};
    image_data.md5 = md5;
    image_data.url = config.Url + "/" + md5;
    image_data.title = title;
    image_data.type = type;
    if(fbx_data != null && image_data != null){
        $("#submit").prop('disabled', false);
    }
}
function upload_fbx(){
    let id = "#uploadform-fbx";
    if($(id).val() !== ""){
        let files = $(id).prop('files');
        if(files.length == 1){
            // let file = files[0];
            $(id).prop('disabled', true);
            print_fbx('开始上传');
            file_compressed(files[0], 
                function(p){
                    print_fbx('压缩文件'+p.toFixed()+"%");
                },
                function(file){
                    file_md5(file, 
                        function(p){
                            print_fbx('分析文件'+p.toFixed()+"%");
                        }, 
                        function(md5){
                             print_fbx('分析完成');
                             console.log("file:"+md5);
                             file_has(md5, function(has){
                                print_fbx('检查文件是否存在');
                                if(has){
                                    fbx_submit( md5,file.name, ".fbx.zip");
                                }else{
                                    file_upload(md5, file, function(p){
                                        print_fbx('上传文件'+p.toFixed()+"%");
                                    }, function(){
                                        fbx_submit( md5, file.name, ".fbx.zip");
                                    });
                                }
                             })
                        }
                    )
                }
            
            )
           
        }
     
    }
}
function upload_image(){

    let id = "#uploadform-image";
    
    if($(id).val() !== ""){
        let files = $(id).prop('files');
        if(files.length == 1){
            let file = files[0];
            $(id).prop('disabled', true);
            print_image('开始上传');

             file_md5(file, 
                function(p){
                    print_image('分析文件'+p.toFixed()+"%");
                }, 
                function(md5){
                     print_image('分析完成');
                     console.log("file:"+md5);
                     file_has(md5, function(has){
                        print_image('检查文件是否存在');
                        if(has){
                            image_submit(md5, file.name,file.type);
                        }else{
                            file_upload(md5, file, function(p){
                                print_image('上传文件'+p.toFixed()+"%");
                            }, function(){
                                image_submit(md5, file.name, file.type);
                            });
                        }
                     })
                }
            )
        }
     
    }
    
}



JS;
$this->registerJsFile("@web/js/cos-js-sdk-v5.js");
$this->registerJsFile("@web/js/file.js"."?".time());
$this->registerJsFile("@web/js/file-local.js"."?".time());
$this->registerJsFile("@web/js/jszip/jszip.js");
$this->registerJsFile("@web/js/spark-md5.js");
$this->registerJs($js, \yii\web\View::POS_BEGIN);
?>
<div class="site-index">
    <div class="jumbotron">
        <h1>模型上传</h1>
    </div>
    <?php
    if (!Yii::$app->user->isGuest) { ?>
        <div class="body-content">
            <div>
                <?php $form = ActiveForm::begin(['action' => ['#'], 'options' => ['enctype' => 'multipart/form-data', 'onsubmit'=>'return false']]) ?>
                <?= $form->field($UploadForm, 'fbx' )->fileInput(['onchange'=>'upload_fbx()'])->label("上传 FBX 文件") ?>
                <?= $form->field($UploadForm, 'image')->fileInput(['onchange'=>'upload_image()'])->label("上传图片") ?>
                <button id="submit" onclick="submit_file()"  disabled="disabled" class="btn btn-default btn-lg">上传</button>
              
                <?php ActiveForm::end() ?>
            </div>
        </div>
    <?php } ?>
</div>



